
$('.pages').on('click', 'a', function () {
    var page = $(this).attr('data-page');
    loadOrderList(page);
});
//加载用户当前的订单列表
loadOrderList();



function loadOrderList(page) {
    if (!page) page = 1;
    obajax.get(
        '/api/user/order',
        {page: page, limit: 5 },
        (result) => {
            var html = '';
            $.each(result.data.list, (index, order) => {
                var sum = 0;
                $.each(order.details, (index, detail) => {
                    sum += detail.count * detail.goods.price;
                });
                html +=
                    '                            <tr class="hd order-meta">\n' +
                    '                                <td colspan="4">\n' +
                    '                                    <div class="left">' + order.createTime + ' | ' + order.addrDetail + ' | 订单号: ' + order.id + ' | ' + ['未支付', '余额支付', '支付宝', '微信', '银联', '其它'][order.payType != null ? (order.payType + 1) : 0] + '</div>\n' +
                    '                                </td>\n' +
                    '                            </tr>\n' +
                    '                            <tr class="order-goods">\n' +
                    '                                <td>';

                $.each(order.details, (index, detail) => {
                    html +=
                        '                                    <div class="goods-info">\n' +
                        '                                        <a href="/goods?id=' + detail.goods.id + '"><img class="figure" src="/upload/' + detail.goods.pics[0].url + '" alt="" /></a>\n' +
                        '                                         <a class="text-info refund" href="uc-apply-refund.html">申请退货</a>\n' +
                        '                                        <div class="info">\n' +
                        '                                            <div><a href="/goods?id=' + detail.goods.id + '">' + detail.goods.name + '</a> ' + detail.goods.author + ' ' + detail.goods.press + ' </div>\n' +
                        '                                            <div>' + detail.price + '元×' + detail.count + '</div>\n' +
                        '                                        </div>\n' +
                        '                                    </div>';
                })
                //0 立即付款 1 退款
                html +=
                    '                                </td>\n' +
                    '                                <td>\n' +
                    '                                    金额：<span class="text-theme fwb">' + sum + '元</span>\n' +
                    '                                </td>\n' +
                    '                                <td>\n' +
                    '                                    <span class="status">'+['未支付','已支付','已发货','已收货','退货退款','退换货','仅退款','售后','其它'][order.status]+'</span><br />\n' +
                    '                                    <a class="text-info" href="/user/order/orderDetail?id='+order.id+'" target="_blank">订单详情</a><br />\n' +
                    '                                    <a class="text-info" href="">查看物流</a>\n' +
                    '                                </td>\n' +
                    '                                <td>';
                switch (order.status) {
                    case 0://未支付
                        html += '<a href="/user/order/pay?id='+order.id+'" target="_blank" class="ui-btn-theme uc-btn-md">立即付款</a>';
                        break;
                    case 1://已支付
                        html += '<a href="" class="ui-btn-theme uc-btn-md">退款</a>';
                        break;
                    case 2://已发货
                        html += '<a href="" class="ui-btn-theme uc-btn-md">仅退款</a>';
                        html += '<a href="" class="ui-btn-theme uc-btn-md">确认收货</a>';
                        break;
                    case 3://已收货
                        html += '<a href="" class="ui-btn-theme uc-btn-md">退货</a>';
                        html += '<a href="" class="ui-btn-theme uc-btn-md">退换货</a>';
                        html += '<a href="" class="ui-btn-theme uc-btn-md">仅退款</a>';
                        html += '<a href="" class="ui-btn-theme uc-btn-md">售后</a>';
                        break;
                    case 4://退货退款
                        break;
                    case 5://退换货
                        break;
                    case 6://仅退款
                        break;//售后
                    case 7:
                        break;
                    case 8://其它
                        break;

                }

                html +=
                    '                                </td>\n' +
                    '                            </tr>';
            });
            $('.uc-table').html(html);

            //渲染分页
            showPageBar(result.data);
        }
    )
    function showPageBar(pageInfo) {
        var html = '';
        if (pageInfo.hasPreviousPage) {
            html += '<a class="page prev" href="javascript:;" data-page="' + pageInfo.prePage + '">上一页</a>';
        } else {
            html += '<a class="page prev disable1" href="javascript:;">上一页</a>';
        }

        $.each(pageInfo.navigatepageNums, (index, num) => {
            if (num == pageInfo.pageNum) {
                html += '<a class="page cur-page" href="javascript:;">' + num + '</a>';
            } else {
                html += '<a class="page" href="javascript:;" data-page="' + num + '">' + num + '</a>';
            }
        })

        if (pageInfo.hasNextPage) {
            html += '<a class="page next" href="javascript:;"  data-page="' + pageInfo.nextPage + '">下一页</a>';
        } else {
            html += '<a class="page next disable1" href="javascript:;">下一页</a>';
        }
        $('.pages').html(html);
    }
}